export default function mixinBlobConnection (Incident) {
  Incident.prototype.addBlobConnection = function(connection, self) {
    if (connection.objectData && this.objectData.bullet === connection.objectData.bullet) {
      // console.log("You're doing it wrong");
    }

    // 链接
    var stick = new THREE.Mesh(
      new THREE.BoxGeometry(1,1,1),
      new THREE.MeshBasicMaterial({ vertexColors: THREE.VertexColors })
    );

    stick.frustumCulled = false;

    var stickContainer = new THREE.Object3D();
    stickContainer.tick = self.stickTick;
    stickContainer.stick = stick;
    stickContainer.start = this;
    stickContainer.end = connection;

    if (this.connections.length > 6) {
      var nearest = this.connections[1];
      var minLen = 1/0;
      for (var i=1; i<this.connections.length; i++) {
        var obj = this.connections[i];
        var len = obj.position.distanceTo(connection.position);
        if (minLen > len && obj.objectData.bullet !== connection.objectData.bullet) {
          minLen = len;
          nearest = obj;
        }
      }
      stickContainer.start = nearest;
    }

    this.connections.push(connection);
    var color1 = stickContainer.end.material.color.clone();
    var color2 = stickContainer.start.material.color.clone();

    stick.endColor = color1;
    stick.startColor = color2;

    var faceIndices = [ 'a', 'b', 'c' ];
    for ( var i = 0; i < stick.geometry.faces.length; i ++ ) {
      var f  = stick.geometry.faces[ i ];
      for( var j = 0; j < 3; j++ ) {
        var vertexIndex = f[ faceIndices[ j ] ];
        var p = stick.geometry.vertices[ vertexIndex ];
        f.vertexColors[ j ] = p.z < 0.0 ? color1 : color2;
      }
    }

    stick.geometry.colorsNeedUpdate = true;

    stickContainer.add(stick);

    this.sticks.push(stickContainer);
    this.sticksContainer.add(stickContainer);
  };
}

